package com.shujia.mr.tuijian;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class PersonMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //a b c d e f g
        //1、如何构建一个组合名字
        //2、如何得到直接好友和间接好友
        String line = value.toString();
        String[] nameArray = line.split(" ");

        //直接好友 1
        for (int i = 1; i < nameArray.length; i++) {
            String nameGroup = formatStr(nameArray[0], nameArray[i]);
            context.write(new Text(nameGroup), new LongWritable(1L));
        }

        //间接好友
        for (int i = 1; i < nameArray.length - 1; i++) {
            for (int j = i + 1;j<nameArray.length;j++) {
                String nameGroup = formatStr(nameArray[i], nameArray[j]);
                context.write(new Text(nameGroup), new LongWritable(0L));
            }
        }

    }

    //定义一个方法，传入两个字符串，出去一种组合
    // apple hello
    public String formatStr(String s1, String s2) {
        if (s1.compareTo(s2) > 0) {
            return s2 + "-" + s1; // apple-hello
        } else {
            return s1 + "-" + s2; // apple-hello
        }
    }
}
